﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digit.Core;
using SMS.UserManager;
using SMS.ServiceManager;

namespace SMS.Admin.Modules
{
    public partial class DetailOtherService : WebControlBase
    {
        private int OtherServiceID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            OtherServiceID = Request.QueryString["otherServId"] != null ? Int32.Parse(Request.QueryString["otherServId"].ToString().Trim()) : -1;

            if (ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1)
            {
                if (OtherServiceID == -1)
                {
                    lnkAdd.Enabled = false;
                    lnkAdd.Enabled = false;
                }
            }

            if (this.IsPostBack) return;

            if (OtherServiceID > 0)
            {
                this.BindObject();
            }
        }

        private void BindObject()
        {
            if (OtherServiceID != -1)
            {
                ServiceOthersInfo objService = ServiceOthersManager.Select(OtherServiceID);
                if (objService != null)
                {
                    txtCmd.Text = objService.Cmd;
                    txtCode.Text = objService.Code;
                    txtParam.Text = objService.Param;

                    txtContent1.Text = objService.Content1;
                    txtContent2.Text = objService.Content2;
                    txtContent3.Text = objService.Content3;
                    txtContent4.Text = objService.Content4;
                    txtContent5.Text = objService.Content5;

                    ddlContentType.SelectedValue = objService.ContentType.ToString();
                    ddlServiceID.SelectedValue = objService.ServiceID;
                    ddlMsgType.SelectedValue = objService.MsgType.ToString();

                    txtTotalMsg.Text = objService.TotalMsg.ToString();
                }
            }
        }

        protected void lnkCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?module=ListOtherService");
        }

        protected void lnkAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (OtherServiceID != -1)
                {
                    ServiceOthersInfo objService = ServiceOthersManager.Select(OtherServiceID);
                    if (objService != null)
                    {
                        objService.Cmd = txtCmd.Text;
                        objService.Code = txtCode.Text;
                        objService.Param = txtParam.Text;

                        objService.Content1 = txtContent1.Text;
                        objService.Content2 = txtContent2.Text;
                        objService.Content3 = txtContent3.Text;
                        objService.Content4 = txtContent4.Text;
                        objService.Content5 = txtContent5.Text;


                        objService.ContentType = byte.Parse(ddlContentType.SelectedValue);
                        objService.MsgType = byte.Parse(ddlMsgType.SelectedValue);
                        objService.ServiceID = ddlServiceID.SelectedValue;

                        if (byte.Parse(txtTotalMsg.Text.Trim()) != 0)
                            objService.TotalMsg = byte.Parse(txtTotalMsg.Text.Trim());

                        else
                        {
                            if (txtContent1.Text.Trim() != "" && txtContent2.Text.Trim() != "" && txtContent3.Text.Trim() != "")
                                objService.TotalMsg = 3;

                            else if ((txtContent1.Text.Trim() != "" && txtContent2.Text.Trim() != "") || (txtContent1.Text.Trim() != "" && txtContent3.Text.Trim() != "") || (txtContent2.Text.Trim() != "" && txtContent3.Text.Trim() != ""))
                                objService.TotalMsg = 2;

                            else
                                objService.TotalMsg = 1;
                        }

                        bool result = ServiceOthersManager.Update(objService);
                        if (result)
                            lblError.Text = "Cập nhật thành công!";
                        else
                            lblError.Text = "Cập nhật lỗi!";
                    }
                }
                else
                {
                    ServiceOthersInfo objService = new ServiceOthersInfo();

                    objService.Cmd = txtCmd.Text;
                    objService.Code = txtCode.Text;
                    objService.Param = txtParam.Text;

                    objService.Content1 = txtContent1.Text;
                    objService.Content2 = txtContent2.Text;
                    objService.Content3 = txtContent3.Text;
                    objService.Content4 = txtContent4.Text;
                    objService.Content5 = txtContent5.Text;

                    objService.ContentType = byte.Parse(ddlContentType.SelectedValue);
                    objService.MsgType = byte.Parse(ddlMsgType.SelectedValue);
                    objService.ServiceID = ddlServiceID.SelectedValue;

                    if (byte.Parse(txtTotalMsg.Text.Trim()) != 0)
                        objService.TotalMsg = byte.Parse(txtTotalMsg.Text.Trim());
                    else
                    {
                        if (txtContent1.Text.Trim() != "" && txtContent2.Text.Trim() != "" && txtContent3.Text.Trim() != "")
                            objService.TotalMsg = 3;

                        else if ((txtContent1.Text.Trim() != "" && txtContent2.Text.Trim() != "") || (txtContent1.Text.Trim() != "" && txtContent3.Text.Trim() != "") || (txtContent2.Text.Trim() != "" && txtContent3.Text.Trim() != ""))
                            objService.TotalMsg = 2;

                        else
                            objService.TotalMsg = 1;
                    }

                    bool result = ServiceOthersManager.Insert(objService);
                    if (result)
                        lblError.Text = "Thêm mới thành công!";
                    else
                        lblError.Text = "Thêm mới lỗi!";
                }
            }
            catch { }
        }

        protected void lnkClear_Click(object sender, EventArgs e)
        {
            txtCmd.Text = "";
            txtCode.Text = "";
            txtParam.Text = "";
            txtContent1.Text = "";
            txtContent2.Text = "";
            txtContent3.Text = "";
            txtContent4.Text = "";
            txtContent5.Text = "";
            txtTotalMsg.Text = "1";
        }
    }
}
